package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"hello/models"
)

type MainController struct {
	beego.Controller
}

type LoginRegisterController struct {
	beego.Controller
}

func (c *MainController) Get() {

	//1、orm对象
	o := orm.NewOrm()
	//2.结构对象
	user := models.User{
		Name: "cici",
		Pwd:  "123456",
	}
	//3、插入数据库   n 影响数据库几行  err 错误信息
	n, err := o.Insert(&user)
	if err != nil {
		fmt.Println(err)
		return
	}

	//查询

	//orm对象
	//查询的对象
	user2 := models.User{}
	//指定查询对象字段值   就是根据id = 1来查询
	//	user2.Id = 1
	//查询   应该去地址
	//err = o.Read(&user2)
	//根据name查询
	user2.Name = "cici"
	err = o.Read(&user2, "Name")

	//更新
	// orm对象
	//o := orm.NewOrm()
	//需要更新的对象
	user3 := models.User{}
	user3.Id = 1
	//重新赋值
	err = o.Read(&user3)
	if err == nil {
		user3.Name = "coco"
		user3.Pwd = "456123"
		//跟新
		n, err = o.Update(&user3)
	}
	//删除
	//orm对象
	user4 := models.User{}
	user4.Id = 1
	n, err = o.Delete(&user4)
	//删除的对象
	//删除

	if err != nil {
		fmt.Println("查询失败")
		return
	}

	fmt.Println(user2)

	fmt.Println(n)
	c.Data["data"] = "hello cici"
	c.TplName = "test.html"
}

func (c *MainController) Post() {
	c.Data["data"] = "hello momo"
	c.TplName = "test.html"
}



func (c *LoginRegisterController) Get() {
	c.Data["data"] = "hello momo"
	c.TplName = "Register.html"
}

func (c *LoginRegisterController) Post() {
	//获取参数
	number := c.GetString("number")
	pwd := c.GetString("pwd")
	fmt.Println(number, pwd)
	//c.TplName c.Redirect("/login",302)的区别
	//TplName 指定试图文件，同事能给试图传递数据
	//c.Redirect() 跳转  不能传递数据  速度快  第一个参数时url地址，第二个是http状态码
	c.TplName = "Register.html"

}

